package com.cloudm.technician.ui.modules.common.home

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.AppUtils
import com.cloudm.technician.R
import com.cloudm.technician.data.model.VersionInfo
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BasePermissionsFragment
import com.cloudm.technician.ui.modules.cloudm_box.change_box.ChangeBoxActivity
import com.cloudm.technician.ui.modules.cloudm_box.machine_list.MachineListActivity
import com.cloudm.technician.ui.modules.cloudm_box.plate_number.PlateNumberActivity
import com.cloudm.technician.ui.modules.common.about.VersionDialogFragment
import com.cloudm.technician.ui.modules.scan_qr_code.ScanQrCodeActivity
import com.cloudm.technician.ui.service.VersionUpdateService
import com.cloudm.technician.ui.widget.OnceClickListener
import kotlinx.android.synthetic.main.fragment_home.*

/**
 * 作者: wesley
 * 时间: 2017/12/18
 * 描述: ui
 */
class HomeFragment : BasePermissionsFragment<HomePresenter>(), HomeContract.View,
        VersionDialogFragment.OnDownLoadClickListener {

    private var flag = 0

    private val versionName: String by lazy {
        AppUtils.getAppVersionName()
    }

    companion object {
        private const val TAG = "HomeFragment"
        private const val REQUEST_CODE_PERMISSION_FOR_CAMERA = 1
        fun newInstance(flag: Int) = HomeFragment().apply {
            arguments = Bundle().apply {
                putInt("flag", flag)
            }
        }
    }

    override val resource: Int
        get() = R.layout.fragment_home

    override fun initInject() {
        DaggerHomeComponent.builder().appComponent(appComponent).homePresenterModule(
                HomePresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        flag = bundle.getInt("flag", 0)
    }

    override fun initVariables() {

    }

    override fun initViews(view: View) {
        when (flag) {
            0 -> presenter?.checkVersion(versionName)
        }
    }

    override fun initListener() {
        btnCloudmBox.setOnClickListener(OnCeClickListener())
        btnChangeBox.setOnClickListener(OnCeClickListener())
        btnScanPlateNumber.setOnClickListener(OnCeClickListener())
    }


    private inner class OnCeClickListener : OnceClickListener() {
        override fun onOnceClick(view: View) {
            when (view.id) {
                R.id.btnCloudmBox -> mContext.openActivity(MachineListActivity::class.java, false)
                R.id.btnChangeBox -> mContext.openActivity(ChangeBoxActivity::class.java, false)
                R.id.btnScanPlateNumber -> requestPermissionsForScanQrCode()
            }
        }
    }

    override fun checkVersionSuccess(info: VersionInfo) {
        presenter?.checkShouldUpdate(info, versionName)
    }

    override fun showUpdateDialog(info: VersionInfo) {
        val fragment = VersionDialogFragment.newInstance(info)
        fragment.listener = this
        fragment.show(childFragmentManager, TAG)
    }

    override fun onDownLoadClick(url: String?) {
        val intent = Intent(mContext, VersionUpdateService::class.java).apply {
            putExtras(Bundle().apply {
                putString("url", url)
            })
        }
        mContext.startService(intent)
    }

    override fun scanRrCodeGrantedSuccess() {
        super.scanRrCodeGrantedSuccess()
        val intent = Intent(mContext, ScanQrCodeActivity::class.java).apply {
            putExtras(Bundle().apply {
                putInt("flag", 3)
            })
        }
        startActivityForResult(intent, REQUEST_CODE_PERMISSION_FOR_CAMERA)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        when (requestCode) {
            REQUEST_CODE_PERMISSION_FOR_CAMERA -> {
                presenter?.dealWithScanResult(data?.extras?.getString("result"))
            }
        }
    }

    override fun showScanResultEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.scan_result_is_null))
    }

    override fun showLicenseFormatMsg() {
        mContext.toast(mContext.resources.getString(R.string.license_format_error))
    }

    override fun showRightLicense(license: String) {
        val intent = Intent(mContext, PlateNumberActivity::class.java).apply {
            putExtras(Bundle().apply {
                putString("license", license)
            })
        }
        mContext.openActivity(intent, false)
    }

}